/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023.
 * Description:  common cmdline processing file
 * Author: pengyunlong <pengyunlong@huawei.com>
 * Create: 2023-11-18
 */

#include <linux/types.h>
#include <linux/libfdt.h>
#include <mach/be8_compatible.h>
#include <asm/io.h>

static inline char tolower(const char c)
{
	return (unsigned char)c | 0x20;
}

unsigned long parse_mem(char *p, int *size)
{
	int base = 10;  // decimal by default
	unsigned long num = 0;
	char *input = p;

	while (*input == ' ' || *input == '=' || *input == ',')
		input++;

	/* parse radix */
	if (*input == '0') {
		input++;
		if (tolower(*input) == 'x') {
			input++;
			base = 16;
		} else {
			base = 8;
		}
	}

	while (*input != ',' && *input != ' ' && *input != '\0') {
		char c = *input++;
		char lower_c = tolower(c);
		int val;

		if ('0' <= c && c <= '9')
			val = c - '0';
		else if ('a' <= lower_c && lower_c <= 'f')
			val = lower_c - 'a' + 10;
		else
			return 0;

		num = num * base + val;
	}
	*size = input - p;

	return num;
}

char *find_str_in_bootargs(char *bootargs, const char *target)
{
	char *str = bootargs;

	while (*str) {
		if (memcmp(str, target, strlen(target)) == 0) {
			if ((str != bootargs) &&
				(*(str - sizeof(char)) != ' ')) {
				str++;
				continue;
			}
			if ((*(str + strlen(target)) != ' ') &&
				(*(str + strlen(target)) != '\0') &&
				(*(str + strlen(target)) != '=')) {
				str++;
				continue;
			}
			return str;
		}
		str++;
	}
	return NULL;
}
